home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Applications / Argus TE 2.0 / Argus Libraries 2.0 / FnMisc.cp < prev    next >
Encoding:
Text File  |  1995-06-28  |  4.7 KB  |  173 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  
  3.     FnMisc.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     Here are some miscellaneous functions which tend to be used a lot.
  9.     
  10.     Functions Include:
  11.     
  12.       FnMisc_ReadPrefs          Reads string from res, converts to int
  13.       FnMisc_SavePrefs          Stores int as a string in res fork
  14.       FnMisc_ColorAvailability  Checks for Color QuickDraw
  15.       FnMisc_GetPixelDepth      Returns current monitor setting
  16.       FnMisc_FrameButton        Frames default dialog button
  17. */
  18.  
  19. // Prototypes
  20.  
  21. long    FnMisc_ReadPrefs         ( int prefStrID );
  22. void    FnMisc_SavePrefs         ( int prefStrID, long value );
  23. Boolean FnMisc_ColorAvailability ( void );
  24. int     FnMisc_GetPixelDepth     ( GDHandle theDevice );
  25. void    FnMisc_FrameButton       ( DialogPtr theDialog,
  26.                                    short buttonID );
  27.  
  28. /********** ReadPrefs */
  29.  
  30. long FnMisc_ReadPrefs( int prefStrID )
  31. /*
  32.     Reads in a string from resource fork specified by prefStrID, 
  33.     converts it to an integer, and returns result.
  34. */
  35. {
  36.     StringHandle  prefStrH;
  37.     Str255        prefStr;
  38.     unsigned char *tempStr;
  39.     int           strLength, defaultResult, i;
  40.     long          result;
  41.     
  42.     defaultResult = 0;
  43.     if( (prefStrH = GetString( prefStrID )) == NULL )
  44.     {
  45.         result = defaultResult;
  46.     }
  47.     else
  48.     {
  49.         HLock( (Handle)prefStrH );
  50.         strLength = (int)(**prefStrH);
  51.         tempStr = *prefStrH;
  52.         for( i=0; i<=strLength; i++ )
  53.         {
  54.             prefStr[i] = tempStr[i];
  55.         }
  56.         StringToNum( prefStr, &result );
  57.         HUnlock( (Handle)prefStrH );
  58.     }
  59.     return result;
  60. }
  61.  
  62.  
  63. /********** SavePrefs */
  64.  
  65. void FnMisc_SavePrefs( int prefStrID, long value )
  66. /*
  67.     Takes a value, converts it into an string, and saves it into an
  68.     existing resource 'STR' identified by prefStrID.  The length of
  69.     the string must be less than or equal to the existing string
  70.     length.  If smaller, string is padded with leading spaces so
  71.     string length in resource is left unchanged.
  72. */
  73. {
  74.     StringHandle  prefStrH;
  75.     Str255        prefStr, valueStr;
  76.     unsigned char *tempStr;
  77.     int           strLength, numLength, i;
  78.  
  79.     if( (prefStrH = GetString( prefStrID )) == NULL )
  80.     {
  81.         // do nothing, string doesn't exist (add error routine?)
  82.     }
  83.     else
  84.     {
  85.         HLock( (Handle)prefStrH );
  86.         strLength = (int)(**prefStrH);
  87.         tempStr = *prefStrH;
  88.         NumToString( value, valueStr );
  89.         numLength = (int)(*valueStr);
  90.         if( numLength <= strLength )
  91.         {
  92.             for( i=1; i<=strLength; i++ )
  93.                 tempStr[i] = ' ';
  94.             for( i=(strLength - numLength + 1); i<=strLength; i++ )
  95.                 tempStr[i] = valueStr[i-(strLength-numLength)];
  96.         }
  97.         ChangedResource( (Handle)prefStrH );
  98.         WriteResource( (Handle)prefStrH );
  99.         HUnlock( (Handle)prefStrH );
  100.     }
  101. }
  102.  
  103.  
  104. /********** ColorAvailability */
  105.  
  106. Boolean FnMisc_ColorAvailability( void )
  107. /*
  108.     Checks to see if the current machine supports Color QuickDraw.  Use
  109.     this routine once at the beginning of your program.
  110. */
  111. {
  112.     SysEnvRec mySystem;
  113.  
  114.     SysEnvirons( 2, &mySystem );
  115.     return( mySystem.hasColorQD );
  116. }
  117.  
  118.  
  119. /********** GetPixelDepth */
  120.  
  121. int FnMisc_GetPixelDepth( GDHandle theDevice )
  122. /*
  123.     Returns the current pixel depth setting of the machine.  Since the
  124.     user can change the setting of the pixel depth on-the-fly (using 
  125.     the 'Monitor' control panel), this routine should be called each
  126.     time you do any drawing.
  127.  
  128.     Example Usage:
  129.         GDHandle  gCurrentDevice;
  130.         int       gPixelDepth;
  131.  
  132.         gCurrentDevice = GetDeviceList();
  133.         gPixelDepth = GetPixelDepth( gCurrentDevice );
  134. */
  135. {
  136.     PixMapHandle screenPMapH;
  137.     int          pixelDepth;
  138.  
  139.     screenPMapH = (**theDevice).gdPMap;
  140.     pixelDepth = (**screenPMapH).pixelSize;
  141.     return( pixelDepth );
  142. }
  143.  
  144.  
  145. /********** FrameButton */
  146. /*
  147.     Frames a button (usually the OK button) in a dialog.
  148. */
  149. void FnMisc_FrameButton( DialogPtr theDialog, short buttonID )
  150. {
  151.     const int kButtonFrameInset = -4;
  152.     const int kButtonFrameSize = 3;
  153.     const int kFilletSize = 16;
  154.  
  155.     short    itemType;
  156.     Rect     itemRect;
  157.     Handle   itemHandle;
  158.     PenState thePnState;
  159.     GrafPtr  oldPort;
  160.     
  161.     GetPort( &oldPort );
  162.     SetPort( theDialog );
  163.     GetDItem( theDialog, buttonID, &itemType, &itemHandle, &itemRect );
  164.     GetPenState( &thePnState );
  165.     PenNormal();
  166.     PenSize( kButtonFrameSize, kButtonFrameSize );
  167.     InsetRect( &itemRect, kButtonFrameInset, kButtonFrameInset );
  168.     FrameRoundRect( &itemRect ,kFilletSize, kFilletSize );
  169.     SetPenState( &thePnState );
  170.     SetPort( oldPort );
  171. }
  172.  
  173. // End of File